﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using util;

namespace WebApplication1.dormitoryAdministrator
{
    public partial class LeaveReport : System.Web.UI.Page
    {
        String hostel;
        protected void Page_Load(object sender, EventArgs e)
        {
            hostel = (String)Application["hostel"];
            SqlDataSource1.SelectCommand = "SELECT * FROM `out_and_stay_application` where type = 1 and hostel = '" + hostel + "'";
            this.init();
        }

        public void init()
        {

            int count = GridView1.Rows.Count;

            for (int i = 0; i < count; i++)
            {
                String state = GridView1.Rows[i].Cells[7].Text;

                if (state != "未审核")
                {
                    Button agree = GridView1.Rows[i].Cells[10].FindControl("agree") as Button;
                    Button refuse = GridView1.Rows[i].Cells[10].FindControl("refuse") as Button;
                    agree.Visible = false;
                    refuse.Visible = false;
                }

            }
        }

        protected void agree_Click(object sender, EventArgs e)
        {
            Button but = (Button)sender;
            GridViewRow gvr = (GridViewRow)but.NamingContainer;
            int row = gvr.RowIndex;
            string id = this.GridView1.DataKeys[row]["id"].ToString();
            Label lab = this.Master.FindControl("username") as Label;
            String sql = "update out_and_stay_application set state = '同意',reviewer = '"+ lab.Text+"' where id = " + id;
            DataTable data = MySqlHelper.GetDataTable(sql);  
            Response.AddHeader("Refresh", "0");
        }


        protected void refuse_Click(object sender, EventArgs e)
        {
            Button but = (Button)sender;
            //如果用的是Button： Button but=(Button)sender
            GridViewRow gvr = (GridViewRow)but.NamingContainer;
            int row = gvr.RowIndex;
            string id = this.GridView1.DataKeys[row]["id"].ToString();
            Label lab = this.Master.FindControl("username") as Label;
            String sql = "update out_and_stay_application set state = '拒绝',reviewer = '" + lab.Text + "' where id = " + id;
            DataTable data = MySqlHelper.GetDataTable(sql);
            Response.AddHeader("Refresh", "0");
        }

        protected void select_Click(object sender, EventArgs e)
        {
            String sql = "SELECT * FROM `out_and_stay_application` where type = 1 and name = '" + selectName.Text+"'";
            DataTable data = MySqlHelper.GetDataTable(sql);
            GridView1.DataSource = data;
            GridView1.DataSourceID = null;
            GridView1.DataBind();
            this.init();
        }

        protected void Audited_Click(object sender, EventArgs e)
        {
            String sql = "SELECT * FROM `out_and_stay_application` where type = 1 and state != '未审核'";
            DataTable data = MySqlHelper.GetDataTable(sql);
            GridView1.DataSource = data;
            GridView1.DataSourceID = null;
            GridView1.DataBind();
            this.init();
        }

        protected void Unreviewed_Click(object sender, EventArgs e)
        {
            String sql = "SELECT * FROM `out_and_stay_application` where type = 1 and state = '未审核'";
            DataTable data = MySqlHelper.GetDataTable(sql);
            GridView1.DataSource = data;
            GridView1.DataSourceID = null;
            GridView1.DataBind();
            this.init();
        }

        protected void selectAll_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = null;
            GridView1.DataSourceID = "SqlDataSource1";
            GridView1.DataBind();
            this.init();
        }
    }
}